#!/usr/bin/env python

# Copyright (c) 2008 Anthony Cramp
# Licensed under the MIT License
# See accompanying file LICENSE.txt or
# http://www.opensource.org/licenses/mit-license.php

class Indent(object):
  """docstring for Indent"""
  def __init__(self, startDepth, indent):
    super(Indent, self).__init__()
    self.indentDepth = startDepth
    self.indent = indent
    
  def get(self):
    """docstring for getIndent"""
    return self.indent*self.indentDepth
    
  def inc(self):
    """docstring for inc"""
    self.indentDepth += 1
    
  def dec(self):
    """docstring for dec"""
    self.indentDepth -= 1
    
def getAttributeString(indent, attribute):
  return "%s(attribute %s reliable timestamp)\n" % (indent.get(),attribute)
  
def getObjectClassString(indent, objectClass):
  ocString = "%s(class %s\n" % (indent.get(), objectClass["name"])
  indent.inc()
  for a in objectClass["attributes"]:
    ocString += getAttributeString(indent, a)
    
  for c in objectClass["children"]:
    ocString += getObjectClassString(indent, c)
    
  indent.dec()
  ocString += "%s)\n" % (indent.get())
  
  return ocString
  
def getParameterString(indent, p):
  return "%s(parameter %s)\n" % (indent.get(), p)
  
def getInteractionClassString(indent, ic):
  icString = "%s(class %s reliable timestamp\n" % (indent.get(), ic["name"])
  
  indent.inc()
  for p in ic["parameters"]:
    icString += getParameterString(indent, p)
    
  for c in ic["children"]:
    icString += getInteractionClassString(indent, c)
    
  indent.dec()
  icString += "%s)\n" % (indent.get())

  return icString
  
def getFed(name, ocs, ics):
  federationString = "(Federation %s)" % (name)

  FEDDIFversionNumber = "v1.3"
  fedVersionString = "(FEDversion %s)" % (FEDDIFversionNumber)

  spacesString = "(spaces )"

  objectClassesString = """
  (objects
    (class ObjectRoot
      (attribute privilegeToDelete reliable timestamp)
      (class RTIprivate)
  """
  
  indent = Indent(3, "  ")

  for c in ocs:
    objectClassesString += getObjectClassString(indent, c)

  objectClassesString += """
    )
  )"""
  

  interactionClassesString = """
  (interactions
    (class InteractionRoot reliable timestamp
      (class RTIprivate reliable timestamp)
  """

  for ic in ics:
    interactionClassesString += getInteractionClassString(indent, ic)

  interactionClassesString += """ 
    )
  )"""

  fedfile = """(FED 
  %s
  %s
  %s
  %s
  %s
)
  """ % (federationString, 
          fedVersionString, 
          spacesString, 
          objectClassesString, 
          interactionClassesString)

  return fedfile
  
def main():
  federationName = "VMS"
  objectClasses = [{"name": "CompositeEntity",
                    "attributes": ["Position", 
                                    "Velocity", 
                                    "Acceleration", 
                                    "Orientation",
                                    "OrientationRate",
                                    "Name",
                                    "Description",
                                    "Affiliation",
                                    "Role",
                                    "DRAlgorithm",
                                    ],
                    "children": [
                                {
                                  "name": "SeaSurface",
                                  "attributes": [],
                                  "children": [],
                                },
                                {
                                  "name": "SubSurface",
                                  "attributes": [],
                                  "children": [],
                                },
                                {
                                  "name": "Air",
                                  "attributes": [],
                                  "children": [],
                                },
                                {
                                  "name": "Land",
                                  "attributes": [],
                                  "children": [],
                                }
                                ]
                }]

  interactionClasses = [
                        {
                        "name": "ExecutionManagement",
                        "parameters": [],
                        "children": 
                          [
                            {
                            "name": "CreateEntity",
                            "parameters": ["FederateName", "ObjectClassName", "ObjectInstanceName", "InitialParameters", "InitialParametersFormat"],
                            "children": []
                            },
                            {
                            "name": "ModifyEntity",
                            "parameters": [],
                            "children": [],
                            },
                            {
                            "name": "RemoveEntity",
                            "parameters": [],
                            "children": [],
                            },
                            {
                            "name": "InitialiseFederate",
                            "parameters": [],
                            "children": [],
                            }
                          ]
                        }
                      ]

  print getFed(federationName, objectClasses, interactionClasses)
  
if __name__ == '__main__':
  main()